package com.chinacoal.microservice.impl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chinacoal.microservice.impl.mapper.ApprovalHistoryMapper;
import com.chinacoal.microservice.impl.mapper.WorkItemMapper;
import com.chinacoal.microservice.impl.service.ApprovalHistoryService;
import com.chinacoal.microservice.impl.util.ProcessFun;
import com.chinacoal.microservice.model.wofkflow.entity.ApprovalHistoryEntity;
import com.chinacoal.microservice.model.wofkflow.entity.WorkItemEntity;
import com.chinacoal.microservice.util.PrimaryKey;

/**  
 * @Package: com.chinacoal.microservice.impl.service.impl.ApprovalHistoryServiceImpl
 * @ClassName: ApprovalHistoryServiceImpl
 * @ClassDesc: 审批历史ServiecImpl
 * @Createby: sunh
 * @CreatedDate: 2019-12-02 16:03:19
 */  
@Service
public class ApprovalHistoryServiceImpl extends ServiceImpl<ApprovalHistoryMapper, ApprovalHistoryEntity> implements ApprovalHistoryService {
	
	@Resource
	private ApprovalHistoryMapper approvalHistoryMapper;
	@Resource
	private WorkItemMapper workItemMapper;

	@Override
	public Page<ApprovalHistoryEntity> selectApprovalHistory(Long instanceId, Integer page, Integer pageSize) throws Exception{
		
		Page<ApprovalHistoryEntity> pages = ProcessFun.getPage(page, pageSize);

		List<ApprovalHistoryEntity> approvalHistoryList = new ArrayList<ApprovalHistoryEntity>();
		
		// 获取到历史
		approvalHistoryList.addAll(approvalHistoryMapper.findList(pages, instanceId));
		
		// 拿到最新待办
		List<WorkItemEntity> workItemList = workItemMapper.getWorkItemByInstanceId(instanceId);
		if(workItemList != null) {
			for (WorkItemEntity workItemModal : workItemList) {
				approvalHistoryList.add(getApprovalHistoryEntity(workItemModal));
			}
		}
		
		return pages.setRecords(approvalHistoryList);
	}

	/**  
	 * @Package: com.chinacoal.microservice.impl.service.impl.ApprovalHistoryServiceImpl.getApprovalHistoryEntity
	 * @MethodName: getApprovalHistoryEntity
	 * @MethodDesc: 工作项转换为审批历史Entity
	 * @ParamList: {
	 *				 @param workItemModal
	 *				 @return
	 *			   }
	 * @Return: ApprovalHistoryEntity
	 * @Createby: sunh
	 * @CreatedDate: 2019-12-02 16:04:57
	 */ 
	private ApprovalHistoryEntity getApprovalHistoryEntity(WorkItemEntity workItemModal) {
		
		Long taskId = workItemModal.getTaskId();
		Long processId = workItemModal.getProcessId();
		Long instanceId = workItemModal.getInstanceId();
		String taskTitle = workItemModal.getTaskTitle();
		Long submitUser = workItemModal.getSubmitUser();
		String submitUserName = workItemModal.getSubmitUserName();
		Long activityInsId = workItemModal.getActivityInsId();
		String activityId = workItemModal.getActivityId();
		String activityName = workItemModal.getActivityName();
		Long activityUser = workItemModal.getActivityUser();
		String activityUserName = workItemModal.getActivityUserName();
		Date startDate = workItemModal.getStartDate();
		String taskDesc = workItemModal.getTaskDesc();
		
		ApprovalHistoryEntity approvalHisModal = new ApprovalHistoryEntity();
		approvalHisModal.setTaskHisId(PrimaryKey.newKey());
		approvalHisModal.setTaskId(taskId);
		approvalHisModal.setProcessId(processId);
		approvalHisModal.setInstanceId(instanceId);
		approvalHisModal.setTaskTitle(taskTitle);
		approvalHisModal.setSubmitUser(submitUser);
		approvalHisModal.setSubmitUserName(submitUserName);
		approvalHisModal.setActivityInsId(activityInsId);
		approvalHisModal.setActivityId(activityId);
		approvalHisModal.setActivityName(activityName);
		approvalHisModal.setActivityUser(activityUser);
		approvalHisModal.setActivityUserName(activityUserName);
		approvalHisModal.setStartDate(startDate);
		approvalHisModal.setTaskDesc(taskDesc);
		
		 return approvalHisModal;
	}
}
